package com.istock.user.controller;

import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.istock.base.common.api.model.ApiBaseResponse;
import com.istock.base.common.api.model.PageInfo;
import com.istock.base.common.api.model.RetStatusEnum;
import com.istock.base.export.util.DataTranslateUtils;
import com.istock.union.user.model.Constants;
import com.istock.union.user.model.OperLog;
import com.istock.union.user.model.SecurityChecker;
import com.istock.union.user.model.UserCredentialVO;
import com.istock.union.user.service.UserCredentialInfoService;
import com.istock.union.user.translate.UserIdTranslateCallback;

@Controller
@RequestMapping("/s/userCredential")
public class UserCredentialController {

	@Autowired
	private UserCredentialInfoService credentialInfoService;
	
	@RequestMapping("showList")
	@SecurityChecker("user:userCredential:search")
	public String shwoList() {
		return "user-app/userCredential/list";
	}
	
	@RequestMapping("search")
	@ResponseBody
	public ApiBaseResponse<PageInfo<UserCredentialVO>> search(String loginName , PageInfo pageInfo){
		if(pageInfo == null) {
			pageInfo = new PageInfo();
		}
		List<UserCredentialVO> credentialList = credentialInfoService.queryUserCredentialByLoginName(loginName, pageInfo);
		
		List result = DataTranslateUtils.translate(credentialList, "userId", "userInfo", new UserIdTranslateCallback());
		
		pageInfo.setData(result);
		return new ApiBaseResponse<PageInfo<UserCredentialVO>>(RetStatusEnum.SUCCESS,Constants.API_RESPONSE_SUCCESS_CODE,  "身份查询成功" , pageInfo);
		
	}
	
	@RequestMapping("remove")
	@ResponseBody
	@OperLog(menuName="身份管理" , functionName="删除")
	@SecurityChecker("user:userCredential:remove")
	public ApiBaseResponse<Boolean> removeById(String ids){
		if(StringUtils.isNotBlank(ids)) {
			List<String> idList = Arrays.asList(ids.split(","));
			credentialInfoService.removeUserCredential(idList);
		}
		return new ApiBaseResponse<Boolean>(RetStatusEnum.SUCCESS,Constants.API_RESPONSE_SUCCESS_CODE,  "删除成功" , true);
	}
	
}
